home *** CD-ROM | disk | FTP | other *** search
/ Speccy ClassiX 1998 / Speccy ClassiX 98.iso / amiga_system / the_aminet / comm / bbs / techusermask.lha / TechUserMask / TechUserMask.c < prev    next >
C/C++ Source or Header  |  1995-05-02  |  7KB  |  215 lines

  1. /* TechUserMask v2.00! Written by Sami Klemola in the year of 1995. */
  2.  
  3. #include "exec/io.h"
  4. #include "exec/memory.h"
  5. #include "exec/libraries.h"
  6. #include "dos/dos.h"
  7. #include "proto/exec_protos.h"
  8. #include "proto/dos_protos.h"
  9. #include "techprg.h"
  10.  
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. typedef unsigned char u_char;
  15.  
  16. extern LONG rawprint(u_char *),print(u_char *,...);
  17.  
  18. int _main(void);
  19. ULONG GetUser(void),input(u_char *);
  20. void ReadFile(u_char *),
  21.     donewmask(void),gethotkey(void),error(u_char *),cleanup(void);
  22.  
  23. extern __far struct Library *DOSBase,*PrivateDosBase;
  24.  
  25. struct RDArgs *ra;
  26. struct UserVal *user;
  27.  
  28. BPTR ifh,file;
  29. ULONG len,cfglen,Mask,newmask,args[6];
  30. u_char *userfile,buf[32],cmd[32],verstr[]="$VER: TechUserMask 2.00",
  31.     template[]="User,MASK/K/N,CFG=CONFIG/K,INTER/S,FULL/S,CHECK/S",
  32.     ERR_CFG[]="invalid configuration entry",
  33.     ERR_MEM[]="unable to allocate memory",*username,*mem,*bits[32],ibuf[32];
  34.  
  35. int _main(void) {
  36. ULONG i,b;
  37. u_char *t;
  38.     PrivateDosBase=DOSBase;
  39.     GetProgramName(cmd,32);
  40.     if(!(ra=ReadArgs(template,&args[0],NULL))) {
  41.         PrintFault(i=IoErr(),cmd);
  42.         exit(i);
  43.     }
  44.     ifh=Input();
  45.     if(!(userfile=AllocVec(2048,MEMF_ANY))) error(ERR_MEM);
  46.     username=(u_char *)args[0];
  47.     if(args[1]) newmask=*((ULONG *)args[1]);
  48.     ReadFile(args[2]?(u_char *)args[2]:(u_char *)"BBS:Cfg/TechMisc.Cfg");
  49.     if(!(t=(u_char *)strstr(mem,"MASKBITS"))) error("configuration entry not found");
  50.     for(i=0;i<32;i++) {
  51.         while((*t!=10)&&*t) t++;
  52.         if(!*t) error(ERR_CFG);
  53.         *t++=0;
  54.         while(*t==10) t++;
  55.         if(!*t) error(ERR_CFG);
  56.         if(*t!='-') bits[i]=t;
  57.     }
  58.     rawprint("TechUserMask v2.00!\n");
  59.     if(args[5]) {
  60.         rawprint("Checking bits...\n\n");
  61.         for(i=0;i<32;i++) if(bits[i])
  62.             print("Mask bit number %ld is used for access to %s\n",i,bits[i]);
  63.             else if(args[4]) print("Mask bit number %ld is unused\n",i);
  64.         rawprint("\nPress any key...");
  65.         i=FGetC(ifh);
  66.         rawprint("\n\n");
  67.     }
  68.     if(!username) rawprint("Viewing users' mask bits...\n\n");
  69.         else print("Searching for %s...\n",username);
  70.     if(!(GetUser())) error("user file not found");
  71.     if(username) if(args[3]) {
  72.         newmask=Mask;
  73.         while(TRUE) {
  74.             print("\fEditing access for %s. Current access:\n\n",buf);
  75.             for(i=0;i<32;i++) if(bits[i])
  76.                 print("%ld\r\t\b\b\b\b%s..................................\r\t\t\t\t%s                              \n",i,bits[i],newmask&(1<<i)?"Yes":"No");
  77.             if(!(input("\n\nEnter the number of the bit to be toggled: ")))
  78.                 break;
  79.             b=atoi(ibuf);
  80.             if(b>31) {
  81.                 rawprint("\n\nValue out of range.\n");
  82.                 gethotkey();
  83.             } else {
  84.                 b=1<<b;
  85.                 if(newmask&b) newmask&=-1-b; else newmask|=b;
  86.             }
  87.         }
  88.         if(newmask!=Mask) donewmask();
  89.     } else {
  90.         print("Mask bits for %s are 0x%lx.\n%s has access to:\n\n",
  91.             buf,Mask,buf);
  92.         if(!Mask) rawprint("...absolutely nowhere!\n");
  93.             else for(i=0;i<32;i++) if((Mask&(1<<i))&&(bits[i]))
  94.             print("........ %s\n",bits[i]);
  95.         if(args[1]) if(newmask!=Mask) donewmask();
  96.     }
  97.     print("\nTechUserMask is copyright in 1995 by Sami Klemola.\n");
  98.     cleanup();
  99.     return(RETURN_OK);
  100. };
  101.  
  102. void donewmask() {
  103. ULONG i;
  104. u_char *m;
  105.     print("\nAccording to your new specification %s would have access to:\n\n",buf);
  106.     if(!newmask) rawprint("...absolutely nowhere!\n");
  107.         else for(i=0;i<32;i++) if((newmask&(1<<i))&&(bits[i]))
  108.     print("........ %s\n",bits[i]);
  109.     print("\nAre you sure you want to change this user's mask to 0x%lx? ",newmask);
  110.     i=FGetC(ifh);
  111.     if((i==(ULONG)'y')||(i==(ULONG)'Y')) {
  112.         m=((u_char *)&user->Mask)+3;
  113.         for(i=0;i<4;i++) {
  114.             *m--=(u_char)newmask;
  115.             newmask=newmask>>8;
  116.         }
  117.         Seek(file,0,OFFSET_BEGINNING);
  118.         Write(file,userfile,len);
  119.         rawprint("Done");
  120.     } else rawprint("Operation cancelled");
  121. };
  122.  
  123. ULONG GetUser(void) {
  124. struct AnchorPath *anch;
  125. u_char *ch,*bufch,*m;
  126. ULONG err,i;
  127.     if(!(anch=AllocVec(sizeof(struct AnchorPath)+256,MEMF_CLEAR|MEMF_PUBLIC))) error(ERR_MEM);
  128.     anch->ap_Strlen=256;
  129.     err=MatchFirst("BBS:User/#?/User.dat",anch);
  130.     while(!err) {
  131.         if(anch->ap_Info.fib_DirEntryType<0) {
  132.             if(file) Close(file);
  133.             file=Open(anch->ap_Buf,MODE_OLDFILE);
  134.             if(file) {
  135.                 Seek(file,0,OFFSET_END);
  136.                 len=Seek(file,0,OFFSET_BEGINNING);
  137.                 if(Read(file,userfile,len)==len) {
  138.                     ch=userfile;
  139.                     bufch=buf;
  140.                     while(*ch && *ch!=10 && *ch!=13) *bufch++=*ch++;
  141.                     *bufch=0;
  142.                     for(err=0;err<6;ch++,err++)
  143.                         while(*ch && *ch!=10 && *ch!=13) ch++;
  144.                     user=(struct UserVal *)ch;
  145.                     Mask=0;
  146.                     m=((u_char *)&user->Mask);
  147.                     for(i=0;i<4;i++) {
  148.                         Mask=Mask<<8;
  149.                         Mask+=(ULONG)m[i];
  150.                     }
  151.                     if(!username)
  152.                         print("%s\r\t\t\t\t0x%lx\n",buf,Mask);
  153.                     else if(!(strcmp(buf,username))) {
  154.                         MatchEnd(anch);
  155.                         FreeVec(anch);
  156.                         return(TRUE);
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         err=MatchNext(anch);
  162.     }
  163.     MatchEnd(anch);
  164.     FreeVec(anch);
  165.     return(username?FALSE:TRUE);
  166. };
  167.  
  168. void ReadFile(unsigned char *fn) {
  169. BPTR cfgfile;
  170.     if(!(cfgfile=Open(fn,MODE_OLDFILE)))
  171.         error("can not read configuration file in");
  172.     Seek(cfgfile,0,OFFSET_END);
  173.     if(!(cfglen=Seek(cfgfile,0,OFFSET_BEGINNING))) {
  174.         Close(cfgfile);
  175.         error("configuration file is empty");
  176.     }
  177.     if(!(mem=(u_char *)AllocVec(cfglen,MEMF_ANY))) error(ERR_MEM);
  178.     Read(cfgfile,mem,cfglen);
  179.     Close(cfgfile);
  180. };
  181.  
  182. void gethotkey(void) {
  183.     rawprint("\nPress enter... ");
  184.     while(FGetC(ifh)!=10);
  185.     rawprint("\n");
  186. };
  187.  
  188. ULONG input(u_char *query) {
  189. ULONG i,c;
  190. u_char *t;
  191.     rawprint(query);
  192.     t=ibuf;
  193.     i=0;
  194.     while(i<30) if((c=FGetC(ifh))==10) break; else {
  195.         *t++=c;
  196.         i++;
  197.     }
  198.     *t=0;
  199.     return(i);
  200. };
  201.  
  202. void error(unsigned char *errstr) {
  203.     cleanup();
  204.     print("%s: %s\n",cmd,errstr);
  205.     exit(RETURN_FAIL);
  206. };
  207.  
  208. void cleanup(void) {
  209.     if(file) Close(file);
  210.     if(userfile) FreeVec(userfile);
  211.     if(mem) FreeVec(mem);
  212.     FreeArgs(ra);
  213. };
  214.  
  215.